package engine.io;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class PNGWriter {
	
	public void createPNG(int[][] pixels, String filename, int width, int height) {
		BufferedImage img = new BufferedImage(width, height, 
				BufferedImage.TYPE_BYTE_GRAY);
		WritableRaster writer = img.getRaster();
		
		for(int x = 0; x < pixels.length; x++) {
			for(int y = 0; y < pixels[x].length; y++) {
				/*int rgb = pixels[x][y];
				int r = (rgb >> 16) & 0xFF;
				int g = (rgb >>  8) & 0xFF;
				int b = rgb & 0xFF;*/
				int gray = pixels[x][y];
				if (gray > 255) {
					gray = 255;
				}
				writer.setPixel(x, y, new int[]{gray});
			}
		}
		
		try {
			ImageIO.write(img, "png", new File(filename));
			System.out.println("Wrote to file " + filename + ".");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
